package TemplateMethod;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class TextFileReader extends AbstractReader {
    @Override
    protected FileInputStream openFile(String filePath) throws IOException{
        System.out.println("打开文件"+filePath);
        return new FileInputStream(filePath);//创建FileIuputStream实例
    }
    @Override
    protected void readContent(FileInputStream fis) throws IOException{
//        System.out.println("开始读取文件内容");
//        int data;
//        while((data=fis.read())!=-1){
//            System.out.println((char) data);
//        }
//        System.out.println(); 逐个输出
        //字节转字符流
        InputStreamReader isr=new InputStreamReader(fis, StandardCharsets.UTF_8);
        //缓冲字符流，按行读取
        BufferedReader br=new BufferedReader(isr);
        String line;
        while ((line=br.readLine())!=null){
            System.out.println(line);
        }
        br.close();
    }
@Override
    protected void closeFile(FileInputStream fis){
        if(fis!=null){
            try{
                fis.close();
                System.out.println("文件已关闭");
            } catch (Exception e) {
                System.out.println("文件关闭失败"+e.getMessage());
            }
        }

}
}
